function [nFs, mXY, vShut, bReload] = Variable_Duration(bAutoLoad)
% GalvoScanControl
% Scan script template: Variable_Duration
%
% Description:
% Run one pulse train with different pulses durations.
%
% Parameters:                   Default:
%   pulse durations (ms)        1:2:20
%   pulses (#)                  12
%   pulse frequency (Hz)        2
%   voltage (V)                 5
%

% Scan scripts should return four variables (in this order):
%  nFs      The sample rate (1/sec between scan points)
%  mXY      A matrix with two columns in the format [X Y], where
%               X  are x-positions
%               Y  are y-positions
%           The unit of this matrix is millimeters, relative to the origin
%  vShut    Vector with same length as mXY where values indicate shutter
%           open/close state (when a shutter is controlled) or the beam
%           intensity (when the output is directed to an intensity
%           modulated laser module, e.g. Coherent CUBE).
%           Note: Values of vShut should not exceed +/- 5V. Values beyond
%           these limits will be fixed at +/- 5V by the software.
%  bReload  Boolean indicating whether the script should be re-loaded each
%           time before executing. Setting this to true (1) will decrease
%           the repetition rate and should only be used for long programs
%           that are not executed at high frequency.


persistent sDurs sReps sFreq sVolt
bReload = 1;

% Default values
if isempty(sDurs), sDurs = '[1:2:20]'; end
if isempty(sReps), sReps = '12'; end
if isempty(sFreq), sFreq = '2'; end
if isempty(sVolt), sVolt = '5'; end

if ~bAutoLoad
    cAnswers = inputdlg({'Pulse duration (ms)', ...
        'Pulse repetions (#)', ...
        'Pulse frequency (Hz)', ...
        'Intensity (0-5 V)'}, 'Frequency Adaptation', ones(1, 4), ...
        {sDurs, sReps, sFreq, sVolt});
    if isempty(cAnswers), return, end
    
    % For some reason, closing the input dialog takes forever.
    % Placing this pause here prevents this.
    pause(0.1)
    
    sDurs = cAnswers{1};
    sReps = cAnswers{2};
    sFreq = cAnswers{3};
    sVolt = cAnswers{4};
end

vDurs = eval(sDurs);
nReps = str2double(sReps);
nFreq = str2double(sFreq);
nVolt = str2double(sVolt);

% Truncate intensity to 0 - 5 V
if nVolt < 0, nVolt = 0;
elseif nVolt > 5, nVolt = 5; end

% Increase durations vector by the number of pulse repetitions
vDurs = repmat(vDurs, 1, nReps);

% Randomize order of pulse duration presentation
vDurs = vDurs(randperm(length(vDurs)));

% Create a pulse train with all pulse durations in random order
vShut = [];
for d = 1:length(vDurs)
    % Generate analog trace from pulse train parameters
    nPeriod = (1 / nFreq) * 1000; % ms
    nPulseDown = nPeriod - vDurs(d);
    
    % Single pulse
    vOpen = [ones(vDurs(d), 1)] .* nVolt;  % Open period
    vClosed = [zeros(nPulseDown, 1)];       % Close period (0V)
    
    % Insert into global analog vector
    vShut = [vShut; vOpen; vClosed];
end
vShut = [vShut; 0]; % make sure laser turns off

% Position of beam is constant throughout this protocol
mXY = repmat(0, length(vShut), 2);
nFs = 1000;

return

